package com.jzo2o.config;

import com.jzo2o.common.handler.RequestIdHandler;
import com.jzo2o.common.handler.UserInfoHandler;
import com.jzo2o.interceptor.FeignInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * Feign客户端核心配置类
 * 功能：配置Feign接口扫描路径、自定义参数编码器、请求拦截器等，仅在feign.enable=true时生效
 */
@Slf4j
@Configuration
@EnableFeignClients(basePackages = "com.jzo2o.api") // feign 扫描包 - 启用 Feign 客户端功能
@Import({com.jzo2o.utils.MyQueryMapEncoder.class}) // 导入自定义的 Feign 配置类。springboot自动装配所需
@ConditionalOnProperty(prefix = "feign", name = "enable", havingValue = "true") // 开启 feign
public class ClientScanConfiguration {

    /**
     * 注册Feign请求拦截器
     * 作用：在Feign发起远程请求时，自动添加上下文信息（如用户Token、请求ID）到请求头中
     */
    @Bean
    public FeignInterceptor feignInterceptor(UserInfoHandler userInfoHandler, RequestIdHandler requestIdHandler){
        return new FeignInterceptor(userInfoHandler, requestIdHandler);
    }


}
